﻿$.fn.tab = function (options) {
    var settings = $.extend({
        tabHeaderElement: '.tab-header',
        tabContentElement: '.tab-content'
    }, options);

    var base = this;
    var tabHeaders = base.find(settings.tabHeaderElement);
    var tabs = base.find(settings.tabContentElement);



    function showTab(index, callback) {
        if (typeof tabs[index] != undefined) {
            tabs.hide();
            $(tabs[index]).show();
            execCallback(callback);
        }
    }

    function execCallback(callback) {
        if (typeof callback == "function")
            callback();
    }

    function init() {
        if (tabHeaders && tabHeaders.length > 0) {
            tabs.hide();
            tabHeaders.each(function () {
                var tabIndex = tabHeaders.index($(this));
                var handler = $(this);
                $(this).click(function () {
                    showTab(tabIndex, function () {
                        $(settings.tabHeaderElement + ".actived").removeClass("actived");
                        handler.addClass("actived");
                    });
                });
            });
            tabHeaders[0].click();
        }
    }

    return this.each(function () {
        init();
    });
};